package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.SetmealService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        // 新用户数量统计
        HashMap map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        Integer newUser = userMapper.getUserStatistics(map);

        //查询总订单数
        Integer totalOrderCount = orderMapper.getOrdersStatistics(map);

        // 营业额统计
        map.put("status", Orders.COMPLETED);
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        // 每日订单数
        Integer countList = orderMapper.getOrdersStatistics(map);
        // 有效订单数
        Integer validOrderCount = orderMapper.getOrdersStatistics(map);
        double orderCompletionRate = 0;
        double averageOrderValue = 0;
        if (validOrderCount != 0 && totalOrderCount != 0) {
            //订单完成率
            orderCompletionRate = countList.doubleValue() / totalOrderCount;

            //平均客单价  营业额 / 有效订单数
            averageOrderValue = turnover / validOrderCount;
        }

        return BusinessDataVO.builder()
                .newUsers(newUser)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(averageOrderValue)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO overviewOrders() {
        HashMap map = new HashMap();
        //全部订单
        Integer countList = orderMapper.getOrdersStatistics(map);

        //已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelled = orderMapper.getOrdersStatistics(map);

        //已完成数量
        map.put("status",Orders.COMPLETED);
        Integer completed = orderMapper.getOrdersStatistics(map);

        //待派送数量
        map.put("status",Orders.CONFIRMED);
        Integer delivered = orderMapper.getOrdersStatistics(map);

        //待接单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waiting = orderMapper.getOrdersStatistics(map);

        return OrderOverViewVO.builder()
                .allOrders(countList)
                .cancelledOrders(cancelled)
                .completedOrders(completed)
                .deliveredOrders(delivered)
                .waitingOrders(waiting)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO overviewDishes() {
        Integer sold = dishMapper.overviewDishes(1);
        Integer discontinued = dishMapper.overviewDishes(0);

        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO overviewSetmeals() {
        Integer discontinued = setmealMapper.overviewDishes(0);
        Integer sold = setmealMapper.overviewDishes(1);

        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

}
